package adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.lingshijia.R;
import com.qianfeng.lingshijia.WebViewActivity;

import java.util.ArrayList;
import java.util.List;

import entity.TotalList_Report_Entity;
import utils.DownloadUtils;

/**
 * Created by Mr.xingkong on 2016/1/14.
 */
public class TotalListReportAdapter extends BaseAdapter
{

    Context context;
    List<TotalList_Report_Entity.DataEntity.ListEntity> data;
    public TotalListReportAdapter(Context context)
    {
        this.context = context;
        data = new ArrayList<>();
    }

    public void setData( List<TotalList_Report_Entity.DataEntity.ListEntity> data)
    {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addData(List<TotalList_Report_Entity.DataEntity.ListEntity> data)
    {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData(){
        data.clear();
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder vh;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.totallist_report_layout,null);
            vh = new ViewHolder();
            vh.image = (ImageView) convertView.findViewById(R.id.totalReport_Pic);
            vh.title = (TextView) convertView.findViewById(R.id.totalReport_Title);
            vh.reportComment = (TextView) convertView.findViewById(R.id.totalReport_reportComment);
            convertView.setTag(vh);
        }else
        {
            vh = (ViewHolder) convertView.getTag();
        }
        DownloadUtils.LoadPicture(data.get(position).getImg1(),vh.image);
        vh.title.setText(data.get(position).getTitle());
        vh.reportComment.setText(data.get(position).getReportComment());
        //设置点击事件
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("url",data.get(position).getLinkUrl());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder
    {
        ImageView image;
        TextView title,reportComment;
    }
}
